/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.actions.login;

import com.mapping.Empleado;
import com.mapping.TipoLogin;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Ricardo
 */
@Namespace(value = "/")
@Action(value = "altaLoginPre", results = {
    @Result(name = "success", location = "/Login/gestionAgregarLogin.jsp")})
public class AltaLoginActionPre extends ActionSupport{
    
    private Empleado[] empleados;
    private TipoLogin[] tipos;
    
    @Override
    public String execute(){
        
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        
        TypedQuery  e=em.createQuery("SELECT e FROM Empleado e", Empleado.class);        
        List<Empleado> resultList = e.getResultList();
        empleados = new Empleado[resultList.size()];
                
        for(int i=0; i <resultList.size(); i++){                                             
            empleados[i] = resultList.get(i);           
        }  
        
        TypedQuery  et=em.createQuery("SELECT t FROM TipoLogin t", TipoLogin.class);        
        List<TipoLogin> resultListL = et.getResultList();
        tipos = new TipoLogin[resultListL.size()];
                
        for(int i=0; i <resultListL.size(); i++){                                             
            tipos[i] = resultListL.get(i);           
        }  
        
        em.flush();
        tx.commit();
        em.close();  
        return SUCCESS;
    }

    public Empleado[] getEmpleados() {
        return empleados;
    }

    public TipoLogin[] getTipos() {
        return tipos;
    }
        
}
